﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.ComponentModel;
using Tipler.Enumlar;

namespace IKYSWeb.UserControls
{
    public partial class Ascx_AutoComplete : System.Web.UI.UserControl
    {
        public delegate void DegerDegisti(object sender, EventArgs args);

        public event DegerDegisti degerDegisti;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

            }
            txtSorgu.Attributes.Add("data-fonksiyon", hdnAutoCompleteFunction.Value);

            txtSorgu.Attributes.Add("data-personeltipi", hdnPersonelTipi.Value);
            txtSorgu.Attributes.Add("data-personelkadrotipi", hdnPersonelKadroTipi.Value);
            txtSorgu.Attributes.Add("data-personelkadrolukadrosuz", hdnPersonelKadroluKadrosuz.Value);
            txtSorgu.Attributes.Add("data-personeltasrakontrol", hdnPersonelTasraKontrol.Value);
            txtSorgu.Attributes.Add("data-personeldosyakontrol", hdnPersonelDosyaKontrol.Value);

            txtSorgu.Attributes.Add("data-birimtasrakontrol", hdnBirimTasraKontrol.Value);
            txtSorgu.Attributes.Add("data-unvanYetkikontrol", hdnUnvanYetkiKontrol.Value);

            txtSorgu.Attributes.Add("data-silinenlerigetir", hdnSilinenleriGetir.Value);

            txtSorgu.Attributes.Add("data-ilebagligorevyerlerininhepsinigetir", hdnIleBagliGorevYerlerininHepsiniGetir.Value);
            

            txtSorgu.Attributes.Add("data-ustbirimid", hdnUstBirimID.Value);

            txtSorgu.Attributes.Add("data-deger", txtDeger.ClientID);
            txtSorgu.Attributes.Add("data-sorgu", txtSorgu.ClientID);
            txtSorgu.Attributes.Add("data-degerauto", AutoPostBack ? "True" : "False");

            btn_Clear.Attributes.Add("data-sorgu", txtSorgu.ClientID);
            btn_Clear.Attributes.Add("data-deger", txtDeger.ClientID);
            btn_Clear.Attributes.Add("data-degerauto", AutoPostBack ? "True" : "False");


            if (txtDeger.Text != "")
            {
                txtSorgu.CssClass = "autoComplete ac_locked";
                txtSorgu.Attributes.Add("readonly", "readonly");
            }
            else
            {
                txtSorgu.CssClass = "autoComplete";
                txtSorgu.Attributes.Remove("readonly");
            }
        }

        public string Deger
        {
            set
            {
                txtDeger.Text = value;

                if (txtDeger.Text != "")
                {
                    txtSorgu.CssClass = "autoComplete ac_locked";
                    txtSorgu.Attributes.Add("readonly", "readonly");
                }
                else
                {
                    txtSorgu.CssClass = "autoComplete";
                    txtSorgu.Attributes.Remove("readonly");
                }
            }
            get
            {
                return txtDeger.Text;
            }
        }

        public string Sorgu
        {
            set
            {
                txtSorgu.Text = value;
            }
            get
            {
                return txtSorgu.Text;
            }
        }

        public bool ReadOnly
        {
            set
            {
                txtSorgu.ReadOnly = value;
                btn_Clear.Visible = !value;

                if (value)
                {
                    txtSorgu.CssClass = "autoComplete ac_locked";
                    txtSorgu.Attributes.Add("readonly", "readonly");
                }
                else
                {
                    txtSorgu.CssClass = "autoComplete";
                    txtSorgu.Attributes.Remove("readonly");
                }
            }
        }

        public bool AllowEmpty
        {
            set { rfvDeger.Enabled = !value; }
        }

        public Unit Width
        {
            set
            {
                txtSorgu.Width = value;
            }
        }

        public void Clear()
        {
            txtDeger.Text = "";
            txtSorgu.Text = "";
            txtSorgu.ReadOnly = false;
            txtSorgu.CssClass = "autoComplete";
            txtSorgu.Attributes.Remove("readonly");
        }

        public string ValidationGroup
        {
            set { rfvDeger.ValidationGroup = value; }
        }

        public string HataMesaji
        {
            set { rfvDeger.ErrorMessage = value; }
        }

        public bool AutoPostBack
        {
            set
            {
                txtDeger.AutoPostBack = value;
                txtSorgu.Attributes.Remove("data-degerauto");
                txtSorgu.Attributes.Add("data-degerauto", AutoPostBack ? "True" : "False");
            }
            get { return txtDeger.AutoPostBack; }
        }

        public EnumAutoCompleteFunction AutoCompleteFunction
        {
            set
            {
                hdnAutoCompleteFunction.Value = value.GetHashCode().ToString();
                txtSorgu.Attributes.Remove("data-fonksiyon");
                txtSorgu.Attributes.Add("data-fonksiyon", hdnAutoCompleteFunction.Value);
            }
            get
            {
                return (EnumAutoCompleteFunction)Convert.ToInt32(hdnAutoCompleteFunction.Value);
            }
        }


        protected void txtDeger_TextChanged(object sender, EventArgs e)
        {
            if (AutoPostBack)
                degerDegisti(this, null);
        }

        #region GÖREV YERİ ÖZELLİKLERİ

        public int UstBirimID
        {
            set
            {
                hdnUstBirimID.Value = value.ToString();
                txtSorgu.Attributes.Remove("data-ustbirimid");
                txtSorgu.Attributes.Add("data-ustbirimid", hdnUstBirimID.Value);
            }
            get
            {
                return Convert.ToInt32(hdnUstBirimID.Value);
            }
        }

        #endregion


        #region BİRİM ÖZELLLİKLERİ

        public bool BirimTasraKontrol
        {
            set
            {
                hdnBirimTasraKontrol.Value = value.ToString();
                txtSorgu.Attributes.Remove("data-birimtasrakontrol");
                txtSorgu.Attributes.Add("data-birimtasrakontrol", value ? "True" : "False");
            }
            get
            {
                return Convert.ToBoolean(hdnBirimTasraKontrol.Value);
            }
        }

        #endregion

        #region Unvan ÖZELLLİKLERİ

        public bool UnvanYetkiKontrol
        {
            set
            {
                hdnUnvanYetkiKontrol.Value = value.ToString();
                txtSorgu.Attributes.Remove("data-unvanYetkikontrol");
                txtSorgu.Attributes.Add("data-unvanYetkikontrol", value ? "True" : "False");
            }
            get
            {
                return Convert.ToBoolean(hdnUnvanYetkiKontrol.Value);
            }
        }

        #endregion


        #region PERSONEL ÖZELLİKLERİ

        public int PersonelKadroTipi
        {
            set
            {
                hdnPersonelKadroTipi.Value = value.ToString();
                txtSorgu.Attributes.Remove("data-personelkadrotipi");
                txtSorgu.Attributes.Add("data-personelkadrotipi", value.ToString());
            }
            get
            {
                return Convert.ToInt32(hdnPersonelKadroTipi.Value);
            }
        }

        public int PersonelTipi
        {
            set
            {
                hdnPersonelTipi.Value = value.ToString();
                txtSorgu.Attributes.Remove("data-personeltipi");
                txtSorgu.Attributes.Add("data-personeltipi", value.ToString());
            }
            get
            {
                return Convert.ToInt32(hdnPersonelTipi.Value);
            }
        }

        public int PersonelKadroluKadrosuz
        {
            set
            {
                hdnPersonelKadroluKadrosuz.Value = value.ToString();
                txtSorgu.Attributes.Remove("data-personelkadrolukadrosuz");
                txtSorgu.Attributes.Add("data-personelkadrolukadrosuz", value.ToString());
            }
            get
            {
                return Convert.ToInt32(hdnPersonelKadroluKadrosuz.Value);
            }
        }

        public bool PersonelTasraKontrol
        {
            set
            {
                hdnPersonelTasraKontrol.Value = value.ToString();
                txtSorgu.Attributes.Remove("data-personeltasrakontrol");
                txtSorgu.Attributes.Add("data-personeltasrakontrol", value ? "True" : "False");
            }
            get
            {
                return Convert.ToBoolean(hdnPersonelTasraKontrol.Value);
            }
        }

        public bool PersonelDosyaKontrol
        {
            set
            {
                hdnPersonelDosyaKontrol.Value = value.ToString();
                txtSorgu.Attributes.Remove("data-personeldosyakontrol");
                txtSorgu.Attributes.Add("data-personeldosyakontrol", value ? "True" : "False");
            }
            get
            {
                return Convert.ToBoolean(hdnPersonelDosyaKontrol.Value);
            }
        }

        #endregion

        #region Silinenleri Getir

        public bool SilinenleriGetir
        {
            set
            {
                hdnSilinenleriGetir.Value = value.ToString();
                txtSorgu.Attributes.Remove("data-silinenlerigetir");
                txtSorgu.Attributes.Add("data-silinenlerigetir", value ? "True" : "False");
            }
            get
            {
                return Convert.ToBoolean(hdnSilinenleriGetir.Value);
            }
        }

        #endregion
        #region ilebagligorevyerlerininhepsinigetir

        public bool IleBagliGorevYerlerininHepsiniGetir
        {
            set
            {
                hdnIleBagliGorevYerlerininHepsiniGetir.Value = value.ToString();
                txtSorgu.Attributes.Remove("data-ilebagligorevyerlerininhepsinigetir");
                txtSorgu.Attributes.Add("data-ilebagligorevyerlerininhepsinigetir", value ? "True" : "False");
            }
            get
            {
                return Convert.ToBoolean(hdnIleBagliGorevYerlerininHepsiniGetir.Value);
            }
        }

        #endregion

        public string CommandArgument
        {
            set
            {
                hdnCommandArgument.Value = value.ToString();
            }
            get
            {
                return hdnCommandArgument.Value;
            }
        }
    }
}